﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using TSU.FAMC.ComputerEngineering.Classes.Circuits.Readers;
using TSU.FAMC.ComputerEngineering.Classes.OrandTrees.Readers;

namespace CircuitBuilder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (circuitOpenDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            // clearing result box
            resultBox.Clear();

            foreach (var fileName in circuitOpenDialog.FileNames)
            {
                var fileStr = String.Empty;
                try
                {
                    // reading file
                    var streamReader = new StreamReader(fileName);
                    fileStr = streamReader.ReadToEnd();
                }
                catch (Exception ex)
                {
                    resultBox.Text += string.Format("{0}.\r\n Ошибка: не удалось прочитать файл. Подрбнее: {1} \r\n\r\n", fileName, ex.Message);
                    return;
                }


                // building circuit
                var builder = new CircuitReaderPlain();
                builder.Input = fileStr;
                builder.Read();

                if (chbBuildTrees.Checked)
                {
                    // building tree
                    var treeBuilder = new OrandTreeReaderCircuit();
                    treeBuilder.Input = builder.Circuit;
                    treeBuilder.Read();

                    // simplifying tree
                    treeBuilder.Tree.Simplify();

                    resultBox.Text += string.Format("Файл: {0}, логических элементов: {1}, путей: {2}, вершин в И,ИЛИ-дереве: {3}, концевых вершин: {4}\n\n", fileName, builder.GatesCount - builder.Circuit.Inputs.Count, builder.Circuit.CountPaths(), treeBuilder.Tree.CountVertices(), treeBuilder.Tree.Leaves.Count);
                }
                else
                {
                    resultBox.Text += string.Format("Файл: {0}, логических элементов: {1}, путей: {2}\n\n", fileName, builder.GatesCount - builder.Circuit.Inputs.Count, builder.Circuit.CountPaths());
                }
            }
        }
    }
}
